package com.example.account;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.example.account.DataBase.AccountDataBase;
import com.example.account.DataBase.AccountDataDao;

public class MoreActivity extends AppCompatActivity {
    private static AccountDataBase accountDataBase;
    private static AccountDataDao accountDataDao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more);
        accountDataBase=AccountDataBase.getDatabase(this);
        accountDataDao=accountDataBase.getDataDao();

        // 添加底部导航栏
        addBottomNavigation();
    }

    public void more_share(View view) {
        Intent intent=new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT,"快来看看我分享给你的记账软件吧~下载地址为https://gitee.com/mengqihe/accounting.git");
        startActivity(Intent.createChooser(intent,"简约记账"));
    }

    public void more_details(View view) {
        Intent intent=new Intent(this,HistoryActivity.class);
        startActivity(intent);
    }

    public void more_delete(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MoreActivity.this);
        builder.setIcon(R.drawable.more_delete);
        builder.setTitle("温馨提示");
        builder.setMessage("确定要清空数据嘛?");

        builder.setPositiveButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 点击“否”后，弹框消失，用户停留在当前页面
                dialog.dismiss();
            }
        });

        builder.setNegativeButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 清空数据
                accountDataDao.deleteAll();

                // 提示用户已清空历史数据
                AlertDialog.Builder confirmBuilder = new AlertDialog.Builder(MoreActivity.this);
                confirmBuilder.setIcon(R.drawable.more_delete);
                confirmBuilder.setTitle("温馨提示");
                confirmBuilder.setMessage("已清空历史数据");
                confirmBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 点击“确定”后，弹框消失，用户停留在当前页面
                        dialog.dismiss();
                    }
                });
                confirmBuilder.create().show();
            }
        });

        builder.create().show();
    }

    private void addBottomNavigation() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.bottom_navigation_container, new BottomNavigationFragment());
        transaction.commit();
    }

    public void more_back(View view) {
        finish();
    }
}